package net.wsfcp.android.http.mvp.model;

import android.support.annotation.NonNull;

import com.google.gson.Gson;

import net.wsfcp.android.BuildConfig;
import net.wsfcp.android.R;
import net.wsfcp.android.base.BaseApplication;
import net.wsfcp.android.base.BaseModel;
import net.wsfcp.android.bean.requestbean.ReqDrawerBean;
import net.wsfcp.android.bean.resultbean.DrawerBean;
import net.wsfcp.android.http.UrlHelper;
import net.wsfcp.android.http.exception.ExceptionEngine;
import net.wsfcp.android.http.exception.ServerException;
import net.wsfcp.android.http.mvp.subscriber.CommonSubscriber;
import net.wsfcp.android.http.mvp.subscriber.ErrorSubscriber;
import net.wsfcp.android.http.mvp.transformer.CommonTransformer;

import okhttp3.MediaType;
import okhttp3.RequestBody;

//侧边栏菜单
public class DrawerModel extends BaseModel {

    public void getDrawer(@NonNull final DrawerInfoHint infoHint) {
        if (infoHint == null)
            throw new RuntimeException("InfoHint不能为空");

        ReqDrawerBean bean = new ReqDrawerBean(UrlHelper.CLIENT_TYPE, BaseApplication.getContext().getResources().getString(R.string.user_name));
        Gson gson = new Gson();
        String obj = gson.toJson(bean);
        RequestBody body = RequestBody.create(MediaType.parse("application/json; charset=utf-8"), obj);
        httpService.getChannelPageData(body).compose(new CommonTransformer()).subscribe(new CommonSubscriber<DrawerBean>(BaseApplication.getContext()) {
            @Override
            public void accept(DrawerBean drawerBean) {

                infoHint.getDrawerSuccess(drawerBean);
            }


        }, new ErrorSubscriber<Throwable>(BaseApplication.getContext()) {
            @Override
            public void accept(Throwable t) {
                ServerException e = ExceptionEngine.handleException(t);
                infoHint.getDrawerFail(e.message);
            }

        });

    }


    public interface DrawerInfoHint {
        //获取侧边栏菜单回调
        void getDrawerSuccess(DrawerBean drawerBean);

        void getDrawerFail(String str);


    }

}
